# -*-coding:utf-8 -*-
#
# Created on 2019/5/13, by felix
#

from django.urls import re_path
from django.conf.urls import include

from apps.foreignkey.views import BookAPIView, BookGenericAPIView, BookMixinsGenericAPIView, \
    BookListCreateUpdateAPIView, BookGenericViewSet, BookModelViewSet

# 路由组件（了解即可）
# 路由模块：为标准的viewset接口提供路径简写的方式
from rest_framework.routers import SimpleRouter

router = SimpleRouter()
router.register('v6/book', BookModelViewSet)  # self, prefix, viewset, basename=None, base_name=None
router.register('v1/book', BookModelViewSet)
router.register('v2/book', BookModelViewSet)
router.register('v3/book', BookModelViewSet)
router.register('v4/book', BookModelViewSet)
router.register('v5/book', BookModelViewSet)


print(router.urls)

urlpatterns = [

    # 01-APIView
    re_path(r'^v1book/$', BookAPIView.as_view()),
    re_path(r'^v1book/(?P<pk>.*)/$', BookAPIView.as_view()),

    # 02-GenericAPIView
    re_path(r'^v2book/$', BookGenericAPIView.as_view()),
    re_path(r'^v2book/(?P<pk>.*)/$', BookGenericAPIView.as_view()),

    # 03-mixins
    re_path(r'^v3book/$', BookMixinsGenericAPIView.as_view()),
    re_path(r'^v3book/(?P<pk>.*)/$', BookMixinsGenericAPIView.as_view()),

    # 04-generic的工具视图
    re_path(r'^v4book/$', BookListCreateUpdateAPIView.as_view()),
    re_path(r'^v4book/(?P<pk>.*)/$', BookListCreateUpdateAPIView.as_view()),

    # 05-viewset工具集
    # 原来的，View的as_view() ：将get请求映射到视图类的get方法
    # 现在的，ViewSet的as_view({'get': 'my_get_list'}): 将get请求映射到视图类的my_get_list方法
    re_path(r'^v5book/$', BookGenericViewSet.as_view({'get': 'my_get_list'})),
    re_path(r'^v5book/(?P<pk>.*)/$', BookGenericViewSet.as_view({'get': 'my_get_obj'})),

    # modelviewset
    re_path(r'^v6book/$', BookModelViewSet.as_view({'get': 'list', 'post': 'create'})),
    re_path(r'^v6book/(?P<pk>.*)/$', BookModelViewSet.as_view(
        {'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})),

    # 路由模块
    # 方式1：也是添加到urlpattern  推荐！！固定写法
    # 类似于admin 只注册一次
    re_path(r'^', include(router.urls))
]

# 路由模块
# 方式2：子列表添加到urlpattern
# urlpatterns.extend(router.urls)
